package admin

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"lphoto/models"
	"lphoto/utils"
	"lphoto/validations/adminValid"
)

type ProductController struct {
	BaseController
}

func (c *ProductController) Index() {
	data := models.MediaInfoGetAllForAdmin()
	c.Data["data"] = data
	c.TplName = "admin/product/index.tpl"
}

func (c *ProductController) Upload() {
	id, _ := c.GetInt("id")
	beego.Alert(id)
	if id > 0 {
		data := models.MediaInfoGetForId(id)
		c.Data["data"] = data
	}else {
		c.Data["data"] = ""

	}
	c.TplName = "admin/product/upload.tpl"
}

func (c *ProductController) DoUpload(){
	u := adminValid.UploadValidation{}

	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &u); err != nil {
		c.responseFail(err.Error(),"")
	}
	result, msg := u.ValidatorAuto(&u)
	if !result {
		c.responseFail(msg,"")
	}
	_, err := models.MediaInfoAdd(u, c.LoginUser.Id)
	if(err == nil){
		c.responseSuccess("添加成功",1)
	}else{
		c.responseFail(err.Error(),0)
	}
}

func (c *ProductController) GetOss()  {
	config := utils.GetOssConfig()
	c.responseSuccess("获取成功！",config)
}

func (c *ProductController) DelProduct(){
	buf := make([]byte, 1024)
	n,_ := c.Ctx.Request.Body.Read(buf)
	param := make(map[string]int)
	json.Unmarshal(buf[0:n], &param)
	print(c.Ctx.Input.RequestBody)
	beego.Alert(c.Ctx.Input.RequestBody)
	_,err := models.MediaInfoDelProduct(param["id"])
	if err == nil{
		c.responseSuccess("操作成功!","")
	}else{
		c.responseFail(err.Error(),"")
	}
}